package com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 批量计算订单运费结果
 * @author chenms
 * @date 2023/11/29 6:06 PM
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BatchCalcOrderFreightResultBO implements Serializable {


    /**
     * 订单运费
     */
    private List<OrderFreight> orderFreightList;


    /**
     * 获取计算成功的订单运费
     */
    @JSONField(serialize = false, deserialize = false)
    public Map<String, BigDecimal> getOrderFreight() {
        if (CollectionUtils.isEmpty(orderFreightList)) {
            return Collections.emptyMap();
        }
        return orderFreightList.stream().filter(item->BooleanUtil.isTrue(item.getSuccess()))
                .collect(Collectors.toMap(OrderFreight::getOrderSn, OrderFreight::getFreightAmount));
    }


    /**
     * 是否存在计算失败订单
     */
    public boolean existsCalcFailureOrder() {
        if (CollectionUtils.isNotEmpty(orderFreightList)) {
            return orderFreightList.stream().anyMatch(item->!BooleanUtil.isTrue(item.getSuccess()));
        }
        return false;
    }

    /**
     * 获取错误信息
     */
    @JSONField(serialize = false, deserialize = false)
    public String getErrorMessage(Integer maxLength) {
        if (CollectionUtils.isEmpty(orderFreightList)) {
            return StringPool.EMPTY;
        }
        String errorInfo = orderFreightList.stream().filter(item -> !BooleanUtil.isTrue(item.getSuccess()))
                .map(OrderFreight::getErrorInfo).collect(Collectors.joining(StringPool.SEMICOLON));
        if (maxLength != null && maxLength > CommonConstants.ONE) {
            return StrUtil.sub(errorInfo, CommonConstants.ZERO, maxLength);
        }
        return errorInfo;
    }

    /**
     * 订单运费计算结果
     */
    @Data
    public static class OrderFreight {

        /**
         * 运费金额
         */
        private BigDecimal freightAmount;
        /**
         * 订单号
         */
        private String orderSn;

        /**
         * 是否成功
         */
        private Boolean success;
        /**
         * 错误码
         */
        private String errorCode;
        /**
         * 错误信息
         */
        private String errorMessage;

        /**
         * 错误数据
         */
        private Object data;


        /**
         * 获取错误信息
         */
        public String getErrorInfo() {
            StringBuilder errorMsgBuilder = new StringBuilder("订单号").append(orderSn).append("计算运费失败:");
            if (Objects.nonNull(errorMessage)) {
                errorMsgBuilder.append(errorMessage);
            }
            if (ObjectUtils.isNotEmpty(data)) {
                errorMsgBuilder.append(StringPool.BRACKET_START).append(data).append(StringPool.BRACKET_END);
            }
            return errorMsgBuilder.toString();
        }

    }
}
